package com.example.testvolication.pojo.valid.validator;

import com.example.testvolication.pojo.valid.annotation.NameCheck;
import org.springframework.util.StringUtils;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

/**
 * @author zhangs
 * @date 2022/5/18 14:11
 */
public class NameCheckValidator implements ConstraintValidator<NameCheck , String> {
    @Override
    public boolean isValid(String s, ConstraintValidatorContext constraintValidatorContext) {
        if (!StringUtils.isEmpty(s) && s.startsWith("1")){
            return true;
        }
        constraintValidatorContext.disableDefaultConstraintViolation();
        constraintValidatorContext.buildConstraintViolationWithTemplate(String.format("%s 不符合要求" , s)).addConstraintViolation();
        return false;
    }
}
